package org.jeecg.config;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.security.DataSecurity;
import org.jeecg.common.security.DataSecurityLoadService;
import org.jeecg.config.filter.DataSecurityFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据加密
 * @author: qiang.zhou
 * @date: 2024/6/3 15:44
 **/
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "tender.data-security")
@ConditionalOnProperty(name = "tender.data-security.enable", havingValue = "true", matchIfMissing = true)
public class DataSecurityConfiguration {
    @Resource
    private DataSecurityLoadService dataSecurityLoadService;
    @Resource
    private DataSecurity dataSecurity;

    /**
     * 需要忽略进行敏感信息加密的接口, 配置项为tender.data-security.ignore-url-list
     *  url为请求的servletPath
     **/
    private List<String> ignoreUrlList;

    @Bean
    public DataSecurityFilter dataSecurityFilter() {
        log.info("开启页面敏感信息加密组件");
        if (ObjectUtils.isEmpty(ignoreUrlList)) {
            return new DataSecurityFilter(dataSecurityLoadService, dataSecurity);
        }
        return new DataSecurityFilter(dataSecurityLoadService, dataSecurity, ignoreUrlList);
    }

    @Bean
    public FilterRegistrationBean<DataSecurityFilter> dataSecurityRegistrationBean(){
        FilterRegistrationBean<DataSecurityFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(dataSecurityFilter());
        return bean;
    }

    public void setIgnoreUrlList(List<String> ignoreUrlList) {
        this.ignoreUrlList = ignoreUrlList;
    }

}
